<?php
class ventas_servicio extends Controller 
{
	function __construct()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model(array('mtransaccionentrada','mservicios','detallerentradas','mregistro','mtipoentrada','mmotivosalida'));
	
	}
	
	function index()
	{
		
		
		if (@strlen($_POST['solicitud']) > 1) {
			
			$fechar1 = explode('/',$_POST['solicitud']);
			$fecharx1 = $fechar1[2].'-'.$fechar1[0].'-'.$fechar1[1];
			
			$fechar2 = explode('/',$_POST['conctato']);
			$fecharx2 = $fechar2[2].'-'.$fechar2[0].'-'.$fechar2[1];
		
			$this->session->set_userdata('iniciov',$fecharx1);
	        $this->session->set_userdata('finv',$fecharx2);
			
			$this->load->view('main', array('content_page' => 'ventas_servicio', 
			                                'menutop' => 5,
		                                    'var'=>$this->ventaPorEntrada(),
		                                    'var3'=>$this->BasuraPorEntrada(),
		                                    'var2'=>$this->tipoSalida(),
		                                    'var4'=>$this->ConfusionPorEntrada(),
		                                    'var5'=>$this->AsPorEntrada(),
		                                    'var6'=>$this->NdPorEntrada()));
		}
		else 
		{
			
		$this->load->view('main', array('content_page' => 'ventas_servicio',
		                                'menutop' => 5,
		                                'var'=>$this->ventaPorEntrada(),
		                                'var3'=>$this->BasuraPorEntrada(),
		                                'var2'=>$this->tipoSalida(),
		                                'var4'=>$this->ConfusionPorEntrada(),
		                                'var5'=>$this->AsPorEntrada(),
		                                'var6'=>$this->NdPorEntrada()
		                               ));
		}
		
		
	}
	
	  function encodeDataURL($strDataURL, $addNoCacheStr=false) {
            if ($addNoCacheStr==true) {
		    if (strpos(strDataURL,"?")<>0)
			  $strDataURL .= "&FCCurrTime=" . Date("H_i_s");
		     else
			   $strDataURL .= "?FCCurrTime=" . Date("H_i_s");
             }

	        return urlencode($strDataURL);
	
             }

    function datePart($mask, $dateTimeStr) {
             @list($datePt, $timePt) = explode(" ", $dateTimeStr);
            $arDatePt = explode("-", $datePt);
            $dataStr = "";
 
    if (count($arDatePt) == 1) {
        list($year, $month, $day) = $arDatePt;
        
        switch ($mask) {
        case "m": return $month;
        case "d": return $day;
        case "y": return $year;
        }
      
        return (trim($month . "/" . $day . "/" . $year));
    }
    return $dataStr;
}

function renderChart($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode, $registerWithJS) {

	if ($strXML=="")
        $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$strURL\")";
    else
        $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$strXML\")";


    $chartIdDiv = $chartId . "Div";
    $ndebugMode = $this->boolToNum($debugMode);
    $nregisterWithJS = $this->boolToNum($registerWithJS);

$render_chart = <<<RENDERCHART

	<!-- START Script Block for Chart $chartId -->
	<div id="$chartIdDiv" align="center">
		Chart.
	</div>
	<script type="text/javascript">	
		//Instantiate the Chart	
		var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
		$tempData
		//Finally, render the chart.
		chart_$chartId.render("$chartIdDiv");
	</script>	
	<!-- END Script Block for Chart $chartId -->
RENDERCHART;

  return $render_chart;
}


function renderChartHTML($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode) {
  
    $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight . "&debugMode=" . $this->boolToNum($debugMode);
    if ($strXML=="")
     
        $strFlashVars .= "&dataURL=" . $strURL;
    else
     
        $strFlashVars .= "&dataXML=" . $strXML;

$HTML_chart = <<<HTMLCHART
	<!-- START Code Block for Chart $chartId -->
	<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="$chartWidth" height="$chartHeight" id="$chartId">
		<param name="allowScriptAccess" value="always" />
		<param name="movie" value="$chartSWF"/>		
		<param name="FlashVars" value="$strFlashVars" />
		<param name="quality" value="high" />
		<embed src="$chartSWF" FlashVars="$strFlashVars" quality="high" width="$chartWidth" height="$chartHeight" name="$chartId" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
	</object>
	<!-- END Code Block for Chart $chartId -->
HTMLCHART;

  return $HTML_chart;
}


function boolToNum($bVal) {
    return (($bVal==true) ? 1 : 0);
}

  function ventaPorEntrada()
  {

	 	 $records = $this->mtipoentrada->LoadEstatus();
	   
	    $strXML = "";
        $strXML = "<chart caption = 'Ventas Por Tipo Entrada' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
         if($records != false) {
			     foreach($records as $jur) 
			     {	
			     	
			        $cant =$this->mtransaccionentrada->LoadTransacionCantidadVentasServicio($this->session->userdata('iniciov'),$this->session->userdata('finv'),$jur->getIdEstatus()); 	
                    $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			     }
	       }

        $strXML .= "</chart>";
        return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
  }
  
    function BasuraPorEntrada()
  {

	 	 $records = $this->mtipoentrada->LoadEstatus();
	   
	    $strXML = "";
        $strXML = "<chart caption = 'Basura Por Tipo Entrada' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
         if($records != false) {
			     foreach($records as $jur) 
			     {	
			     	
			        $cant =$this->mtransaccionentrada->LoadTransacionCantidadBasuraServicio($this->session->userdata('iniciov'),$this->session->userdata('finv'),$jur->getIdEstatus()); 	
                    $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			     }
	       }

        $strXML .= "</chart>";
        return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
  }
  
      function ConfusionPorEntrada()
  {

	 	 $records = $this->mtipoentrada->LoadEstatus();
	   
	    $strXML = "";
        $strXML = "<chart caption = 'Confusion Por Tipo Entrada' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
         if($records != false) {
			     foreach($records as $jur) 
			     {	
			     	
			        $cant =$this->mtransaccionentrada->LoadTransacionCantidadCofusionServicio($this->session->userdata('iniciov'),$this->session->userdata('finv'),$jur->getIdEstatus()); 	
                    $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			     }
	       }

        $strXML .= "</chart>";
        return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
  }
  
  
        function AsPorEntrada()
  {

	 	 $records = $this->mtipoentrada->LoadEstatus();
	   
	    $strXML = "";
        $strXML = "<chart caption = 'Adquirio el Servicio' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
         if($records != false) {
			     foreach($records as $jur) 
			     {	
			     	
			        $cant =$this->mtransaccionentrada->LoadTransacionCantidadAsServicio($this->session->userdata('iniciov'),$this->session->userdata('finv'),$jur->getIdEstatus()); 	
                    $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			     }
	       }

        $strXML .= "</chart>";
        return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
  }
  
          function NdPorEntrada()
  {

	 	 $records = $this->mtipoentrada->LoadEstatus();
	   
	    $strXML = "";
        $strXML = "<chart caption = 'No Tiene Decision' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
         if($records != false) {
			     foreach($records as $jur) 
			     {	
			     	
			        $cant =$this->mtransaccionentrada->LoadTransacionCantidadN0dServicio($this->session->userdata('iniciov'),$this->session->userdata('finv'),$jur->getIdEstatus()); 	
                    $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			     }
	       }

        $strXML .= "</chart>";
        return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
  }
  

    
  function ventaPorEntrada2()
  {

	 	 $records = $this->mservicios->Loadservicios();
	   
	    $strXML = "";
        $strXML = "<chart caption = 'Ventas Por Tipo Servicio' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
         if($records != false) {
			     foreach($records as $jur) 
			     {	
			     	
			        $cant =$this->mtransaccionentrada->LoadTransacionCantidadVentasServicio($this->session->userdata('iniciov'),$this->session->userdata('finv'),$jur->getIdServicio()); 	
                    $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$cant."' />";
			     }
	       }

        $strXML .= "</chart>";
        return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
  }
  
  
function tipoSalida()
{

	 $records = $this->mmotivosalida->LoadEstatus();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo Salida' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
			$cant =$this->mtransaccionentrada->LoadGraficoSalida($jur->getIdEstatus()); 	
            $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}

function detruir()
           	{
           		$this->session->unset_userdata('iniciov');
           		$this->session->unset_userdata('finv');
           		redirect('ventas_servicio');
           	}
	

}